$(function() {
  localCache.load(true);
  var search=decodeURI(window.location.search.substr(1)),
  companyInfo=JSON.parse(matchQueryString(search,'authData')||{}),
  company=$('#companyName').val(companyInfo.ext_orgName),
  linker=$('#linker').val(companyInfo.ext_orgLinker),
  telephone=$('#telephone').val(companyInfo.ext_orgPhone),
  email=$('#email').val(companyInfo.ext_orgEmail),
  callerInfo=matchQueryString(search,'caller'),
  isadmin=matchQueryString(search,'isadmin'),
  isAdmin=(isadmin===1||isadmin==='1')?true:false,//不设置isadmin参数默认为非管理员
  userId=matchQueryString(search,'ext_terminalCode'),
  yes=$('#yes'),
  accountIdentifer=matchQueryString(search,'app_key'),
  index=matchQueryString(search,'index'),
  supportUap=matchQueryString(search,'supportUap')||'0',
  isLoadContacts=matchQueryString(search,'isLoadContacts')||'0',
  showUnionAccount=matchQueryString(search,'showUnionAccount'),
  isShowUnionAccount=(showUnionAccount===1||showUnionAccount==='1'||showUnionAccount==='')?true:false,//不设置showUnionAccount参数默认显示账户合并页面
  contacts=matchQueryString(search,'called'),
  ext_orgReleCode=matchQueryString(search,'ext_orgReleCode'),
  ensure=$('#ensure').on('click',function(){
    if(!ensure.hasClass('disable')){//可以继续注册
      var n=company.val(),l=linker.val(),t=telephone.val();
      if(!checkNullValues([{value:n,msg:'公司名称不能为空'},{value:l,msg:'管理员姓名不能为空'},{value:t,msg:'手机号码不能为空'}]))
         return;
      var sUrl=matchQueryString(search,'status_url');
      if(sUrl){
    	  sUrl=','+sUrl;
      }
      $.ajax({
        type: "get",
        url:localCache.getSessionItem('raa_url'),
        dataType: "json",
        async:true,
        data:{
          app_key:accountIdentifer,
          sign:matchQueryString(search,'sign'),
          timestamp:matchQueryString(search,'timestamp'),
          ext_orgCode:companyInfo.ext_orgCode,
          authData:'{ext_orgName:"'+n+'",ext_orgLinker:"'+l+'",ext_orgEmail:"'+email.val()+'"}',
          record_url:matchQueryString(search,'record_url'),   
          ivrdigit_url:matchQueryString(search,'ivrdigit_url'),   
          channel_sysCode:matchQueryString(search,'channel_sysCode'),   
          status_url:localCache.getSessionItem('status_url')+sUrl,
          isadmin:isadmin,
          ext_orgPhone:companyInfo.ext_orgPhone,
          ext_orgReleCode:ext_orgReleCode
        },
        complete:function(xhr,status){
          if(loadPrompt){
            loadPrompt.close();
            loadPrompt=null;
          }
        },
        beforeSend:function(){
          loadPrompt=new LoadPrompt('正在注册组织账户，请稍后...');           
        },
        success: function(rc){
          if(rc.result==='0'){//注册成功
            var callerObject=JSON.parse(callerInfo);
            //根据index值路由页面，默认进入欢迎页面
            index=(index==='')?0:Number(index);
            localCache.setSessionItems({
              account_identifer:accountIdentifer,
              timestamp:rc.data.timestamp,
              sign:rc.data.sign,
              periphery_code:rc.data.periphery_code,
              periphery_account:rc.data.cust_account,
              caller:callerObject.telephone,
              callerName:callerObject.name,
              account_identify:rc.data.cust_account,
              companyName:companyInfo.ext_orgName,
              userId:(userId==='')?callerObject.telephone:userId,
              isAdmin:isAdmin,
              index:index,
              contacts:contacts,
              supportUap:supportUap,
              isLoadContacts:isLoadContacts,
              ext_orgReleCode:ext_orgReleCode,
              isShowUnionAccount:isShowUnionAccount
            });
            if(isAdmin){
              localCache.setSessionItems({
                company_code:rc.company_code,
                product_code:rc.product_code,
                region_code:rc.region_code
              });
            }
            router(index);//第一次注册肯定不会存在历史通话记录
          }else{
            new UserPrompt(rc.describe);
          }
        }
      });
    }
  });
  yes.on('click',function(){
    if(yes.hasClass('active')){
      yes.removeClass('active');
      ensure.addClass('disable');
    }else{
      yes.addClass('active');
      ensure.removeClass('disable');
    }
  });
  
  $('#agreement').on('click',function(event){
    event.stopPropagation();
    //打开协议
  });
  
  function checkNullValues(values){
    for(var i=0;i<values.length;i++){
      if(values[i].value===''){
        new UserPrompt(values[i].msg);
        return false;
      }
    }
    return true;
  }
  
 }
)
